package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.HisCircleListAdapter;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.HisCircleBean;
import com.dingwei.lesyndrome.mvp.presenter.HisCirclePresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.jaeger.library.StatusBarUtil;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/3/6.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class HisCircleActivity extends BaseActivity implements Contract.HisCircleView {
    HisCirclePresenter hisCirclePresenter = new HisCirclePresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    HisCircleListAdapter adapter;

    String mid;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_his_circle;
    }

    @Override
    protected void initData() {
        page = 1;
        hisCirclePresenter.initData(page, mid, true, true);
    }

    @Override
    protected void initView() {

        StatusBarUtil.setTranslucentForImageView(this, 30, null);

        mid = getIntent().getStringExtra("mid");
        adapter = new HisCircleListAdapter(BaseActivity.context);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.parting_line), 1));
        mRecyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                hisCirclePresenter.initData(page, mid, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                hisCirclePresenter.initData(page, mid, true, false);
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                HisCircleBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, CircleDetailsActivity.class);
                intent.putExtra("id", bean.id);
                startActivity(intent);
            }
        });
        adapter.xxInterfaceListener(new HisCircleListAdapter.clickInterFace() {
            @Override
            public void onLike(int position) {
                HisCircleBean.DataBean.ListBean bean = adapter.getItem(position);
                hisCirclePresenter.like(bean.id, position);
            }

            @Override
            public void onComment(int position) {

            }

            @Override
            public void onPlay(int position) {
                HisCircleBean.DataBean.ListBean bean = adapter.getItem(position);
                Intent intent = new Intent(BaseActivity.context, VideoPlayerActivity.class);
                intent.putExtra("url", bean.video);
                intent.putExtra("face", bean.vimg);
                startActivity(intent);
            }

        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            dataLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFail() {
        showData("fail");
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();

    }

    @Override
    public void onRefresh(HisCircleBean bean) {
        Glide.with(BaseActivity.context)
                .load(bean.data.minfo.showHportrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);
        tvName.setText(bean.data.minfo.nickname);
        tvPhone.setText(bean.data.minfo.showPhone);

        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData("success");

    }

    @Override
    public void onLoadMore(HisCircleBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void likeSuccess(int positon) {
        HisCircleBean.DataBean.ListBean bean = adapter.getItem(positon);
        bean.isAgree = !bean.isAgree;
        adapter.notifyItemChanged(positon, bean);
    }

    @OnClick({R.id.img_back, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.no_net_btn:
                page = 1;
                hisCirclePresenter.initData(page, mid, true, true);
                break;
        }
    }
}
